package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private HttpSession session;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/page")
    public R<Page<DishDto>> selectAll(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = dishService.selectAllByPage(page, pageSize, name);
        Page<DishDto> dishDtoPage = new Page<>();
        dishDtoPage.setRecords(new ArrayList<DishDto>());

        List<DishDto> records = dishDtoPage.getRecords();
        for (Dish dish : dishPage.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryService.selectByCategoryId(dish.getCategoryId());
            String categoryName = null;
            if (category != null) {
                categoryName = category.getName();
                dishDto.setCategoryName(categoryName);

            }


            records.add(dishDto);
        }

        dishDtoPage.setPage(dishPage.getPage());
        dishDtoPage.setTotal(dishPage.getTotal());
        dishDtoPage.setPageSize(dishPage.getPageSize());

        R<Page<DishDto>> success = R.success(dishDtoPage);
        return success;
    }

    //接收前端请求 status为restful风格传入的菜品状态
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, Long[] ids) {
       /* Dish dish = dishService.selectById(ids);
        Long id = (Long) session.getAttribute("employee");
        //修改菜品售卖状态
        dish.setStatus(status);
        //修改最后更新信息的用户id
        dish.setUpdateUser(id);
        Boolean update = dishService.update(dish);*/

        Boolean flag = dishService.updateStatus(status, ids);


        if (!flag) {
            return R.error("修改失败");
        }

        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id) {
        //根据前端传来的菜品id查找菜品
        Dish dish = dishService.selectById(id);
        //根据菜品id查找对应菜品口味,存入集合
        List<DishFlavor> flavorsByDishID = dishFlavorService.findFlavorsByDishID(id);

        Category category = categoryService.selectTypeById(dish.getCategoryId());
        Integer type = category.getType();

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(flavorsByDishID);//将口味集合存入dishDto
        dishDto.setCategoryName(category.getName());
        return R.success(dishDto);
    }

    @PutMapping()
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        Long employee = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(employee);
        Boolean flag = dishService.updateAll(dishDto);

        if (!flag){
            return R.error("修改失败");
        }

        return R.success("成功");
    }

    @DeleteMapping()
    public R<String> deleteByIds(Long[] ids){
        Boolean flag = dishService.deleteByIds(ids);
        if (!flag){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    @PostMapping()
    public R<String> add(@RequestBody DishDto dishDto,HttpSession session){
        Long employee = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(employee);
        dishDto.setCreateUser(employee);
        System.out.println(dishDto.getName());
        dishService.add(dishDto);

        return R.success("成功");

    }

    @GetMapping("/list")
    public R<List<DishDto>> findBySetmealId(Long categoryId,Integer status){
        List<DishDto> byCategoryIdAndStatus = dishService.findByCategoryIdAndStatus(categoryId, status);
        return R.success(byCategoryIdAndStatus);
    }




}
